var emailflag    = false;
var passwordflag = false;
var nikenameflag = false;
var mobileflag   = false;

function checkEmail()
{
  var Val = $('#email').attr('value');
  var projectid = $('#projectid').attr('value');
  if(IsMail(Val)){
	$.ajax({
	   async:false,
	   type: "GET",
	   url: "/index.php/app/verfiyemail",
	   data: "email="+Val+"&projectid="+projectid,
	   success: function(msg){
		 if(1== msg){
             $('#emailtip').html("<img src='http://mba.mbachina.com/resource/images/yxk_zc_wrong.jpg' /><span style='color:#d55437;'>该用户已存在！</span>");
             $('#email').focus();
             $('#logintip').show();
		 } else {
             $('#emailtip').html("<img src='http://mba.mbachina.com/resource/images/yxk_zc_right.jpg' />");
			 emailflag = true;
             $('#logintip').hidden();
		 }
	   }
	}); 
  } else {
      $('#emailtip').html(" <img src='http://mba.mbachina.com/resource/images/yxk_zc_wrong.jpg' /><span style='color:#d55437;'> 邮箱不正确！</span>");
      $('#email').focus();
      $('#logintip').hidden();
  }
}


function vPassword(){
    var Val1 = $('#password').attr('value');
    var Val2 = $('#repassword').attr('value');
    
    if(Val1==''){
        $('#passwordtip').html(" <img src='http://mba.mbachina.com/resource/images/yxk_zc_wrong.jpg' /><span style='color:#d55437;'>密码不能为空！</span>");
        $('#password').focus();
    } else {
      if(Val1.length<=3){
        $('#passwordtip').html(" <img src='http://mba.mbachina.com/resource/images/yxk_zc_wrong.jpg' /><span style='color:#d55437;'>密码不能少于3个字符！</span>");
        $('#password').focus();
      } else {
        if(Val1 != Val2){
            $('#repasswordtip').html(" <img src='http://mba.mbachina.com/resource/images/yxk_zc_wrong.jpg' /><span style='color:#d55437;'>两次密码不一致，请重新填写！</span>");
            $('#repassword').focus();
        } else {
            $('#passwordtip').html(" <img src='http://mba.mbachina.com/resource/images/yxk_zc_right.jpg' />");
            $('#repasswordtip').html(" <img src='http://mba.mbachina.com/resource/images/yxk_zc_right.jpg' />");
			passwordflag = true;
        }       
      }
    }
}

function vRealName(){
    var Val = $('#realname').attr('value');
    
    if(Val==''){
        $('#realnametip').html(" <img src='http://mba.mbachina.com/resource/images/yxk_zc_wrong.jpg' /><span style='color:#d55437;'>真实姓名不能为空!</span>");
        $('#realname').focus();
    } else {
      if(Val.length<2){
        $('#realnametip').html(" <img src='http://mba.mbachina.com/resource/images/yxk_zc_wrong.jpg' /><span style='color:#d55437;'>真实姓名不能少于2个字符!</span>");
        $('#realname').focus();
      } else {
         $('#realnametip').html("<img src='http://mba.mbachina.com/resource/images/yxk_zc_right.jpg' />");
		nikenameflag = true;
      }
    }
}

function vTel(){
  var Val = $('#tel').attr('value');
    
    if(Val==''){
        $('#teltip').html("<span style='color:#d55437;'> 电话号码不能为空！</span>");
		$('#tel').focus();
    } else {
         $('#teltip').html("<img src='http://mba.mbachina.com/resource/images/yxk_zc_right.jpg' />");
		mobileflag = true;
    }
}

function addUser(){
	var email        = $('#email').attr('value');
	var password     = $('#password').attr('value');
     var repassword  = $('#repassword').attr('value');
	var realname     = $('#realname').attr('value');
	var tel           = $('#tel').attr('value');
	var degree       = $('#degree').attr('value');
	var projectid    = $('#projectid').attr('value');
    
    // 获取选择的学校
    var yxxys = $('#schoolid').val();

    // 开始验证考生填写的信息是否真确
    checkEmail();
    if(!emailflag) {
        return false;
    }
    
    vPassword();
    if(!passwordflag) {
        return false;
    }
    
    vRealName();
    if(!nikenameflag) {
        return false;
    }
    
    vTel();
    if(!mobileflag) {
        return false;
    }

	if(!emailflag || !passwordflag || !nikenameflag || !mobileflag) {
		alert('填写有误，请填写您的正确信息！');
		return false;
	}
    
    if(yxxys == "") {
        alert("你至少要选择一个学校！");
        return false;
    }

	// 提交到数据库
	$.ajax({
	   type: "POST",
	   url: "/index.php/app/saveuser",
	   data: "email="+email+"&password="+password+"&realname="+realname+"&tel="+tel+"&itemid="+projectid+"&yxxys="+yxxys+"&degree="+degree,
	   success: function(msg){
		 if(1== msg){
				alert("报名失败，请重新填写!");
		 } else {
				alert("报名成功！请登录MBA中国网报名服务中心维护自己的信息，查阅录取情况！");
				//window.open("http://user.mbachina.com", "smbalogin", "fullscreen=1");
                  //window.location.href="http://user.mbachina.com";
                  window.location.href="http://user.mbachina.com/login/applogin?email="+email+"&password="+password+"&itemid="+projectid;

		 }
	   }
	}); 

    return true;
}

function IsMail(mail)    //验证邮箱格式 
{
	var patrn = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if (!patrn.test(mail)){
		return false;
	}else{
		return true;
	}
}